package UI.Table;

import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import Task.DSTask;
import UI.DSUILayout;

//@author A0094762W
/**
 * DSTableCellRenderer is a custom TableCellRenderer that draws out the cells
 * of the task list. 
 */
public class DSTableCellRenderer implements TableCellRenderer {
	private HashMap<String, Boolean> _isSimplifiedMap;
	private HashMap<String, DSTaskTableCell> _tableCellMap;
	private DSTaskTableCell _tableCell;
	private boolean _isSimplified;
	
	public DSTableCellRenderer() {
		_isSimplifiedMap = new HashMap<String, Boolean>();
		_tableCellMap = new HashMap<String, DSTaskTableCell>();
		_isSimplified = false;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		DSTask task = (DSTask) value;

		if (_isSimplifiedMap.containsKey(task.getTaskId())) {
			_isSimplified = _isSimplifiedMap.get(task.getTaskId());
		} else {
			_isSimplifiedMap.put(task.getTaskId(), false);
			_isSimplified = false;
		}
		
		if (!_tableCellMap.containsKey(task.getTaskId())) {
			try {
				_tableCell = new DSTaskTableCell(task, row + 1, _isSimplified);
				_tableCellMap.put(task.getTaskId(), _tableCell);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			table.setRowHeight(row, _tableCell.getHeight());
		} else {
			try {
				_tableCell = new DSTaskTableCell(task, row + 1, _isSimplified);
				_tableCellMap.put(task.getTaskId(), _tableCell);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		_tableCell.setOpaque(false);
		return _tableCell;
	}
	
	public void toggleSimplifiedStatus(String taskId) {
		if (_isSimplifiedMap.containsKey(taskId)) {
			boolean tempSimplifiedStatus = _isSimplifiedMap.get(taskId);
			_isSimplifiedMap.put(taskId, !tempSimplifiedStatus);
		}
	}
	
	public void setAllSimplifiedStatus(boolean status) {
		for (Map.Entry<String, Boolean> isSimplified : _isSimplifiedMap.entrySet()) {
	    isSimplified.setValue(status);
		}
	}
	
	public int getTableCellHeight(String taskId) {
		DSTaskTableCell tableCell = _tableCellMap.get(taskId);
		return tableCell.getHeight();
	}
	
	public void addNewTaskToCreateCell(DSTask task, int taskIndex) {
		if (_isSimplifiedMap.containsKey(task.getTaskId())) {
			_isSimplified = _isSimplifiedMap.get(task.getTaskId());
		} else {
			_isSimplifiedMap.put(task.getTaskId(), false);
			_isSimplified = false;
		}
		
		try {
			_tableCell = new DSTaskTableCell(task, taskIndex + 1, _isSimplified);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		_tableCellMap.put(task.getTaskId(), _tableCell);
	}
	
	public void updateCellMap(Object[] rowData) {
		for (int i = 0; i < rowData.length; i++) {
			DSTask task = (DSTask) rowData[i];
			
			if (_isSimplifiedMap.containsKey(task.getTaskId())) {
				_isSimplified = _isSimplifiedMap.get(task.getTaskId());
			} else {
				_isSimplifiedMap.put(task.getTaskId(), false);
				_isSimplified = false;
			}
			
			try {
				_tableCell = new DSTaskTableCell(task, i + 1, _isSimplified);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			_tableCellMap.put(task.getTaskId(), _tableCell);
		}
	}
}
